#include "qcomboboxitemproperty.h"

#include "../qcommonstruct.h"
#include "../qpropertyfactory.h"
#include "../xmlobject.h"

QComboBoxItemProperty::QComboBoxItemProperty(QAbstractProperty *parent):
    QAbstractProperty(parent)
{
    ComboBoxItems items;
    m_value.setValue<ComboBoxItems>(items);
    set_property("type","ComboItem");
}

QIcon QComboBoxItemProperty::get_value_icon()
{
    return QIcon();
}

QString QComboBoxItemProperty::get_value_text()
{
    ComboBoxItems items=get_value().value<ComboBoxItems>();
    if(items.size()==0)
    {
        return "None";
    }
    else
    {
        return QString("Item[%1]").arg(items.size());
    }
}

void QComboBoxItemProperty::toObject(XMLObject *xml)
{
    if(xml!=NULL)
    {
        xml->clear();
        xml->set_title(m_propertys.value("name").toString());

        QMap<QString,QVariant> pros=m_propertys;
        pros.remove("name");
        pros.remove("value");
        QMapIterator<QString,QVariant>      it(pros);

        while(it.hasNext())
        {
            it.next();
            xml->set_property(it.key(),it.value().toString());
        }

        ComboBoxItems items=get_value().value<ComboBoxItems>();

        foreach(tagComboBox item,items)
        {
            XMLObject* o=new XMLObject(xml);
            o->set_title("Item");
            o->set_property("Text",item.m_text);
            o->set_property("Icon",item.m_icon);
        }
    }
}

void QComboBoxItemProperty::fromObject(XMLObject *xml)
{
    if(xml!=NULL)
    {

        QMapIterator<QString,QString>      it(xml->get_propertys());

        while(it.hasNext())
        {
            it.next();
            m_propertys.insert(it.key(),it.value());
        }

        ComboBoxItems items;

        foreach(XMLObject *o,xml->get_children())
        {
            tagComboBox item;
            item.m_icon=o->get_property("Icon");
            item.m_text=o->get_property("Text");
            items.append(item);
        }
        m_value.setValue<ComboBoxItems>(items);
        refresh();
    }
}

QString QComboBoxItemProperty::get_property_name()
{
    return "ComboItem";
}

bool QComboBoxItemProperty::cmp(const QVariant &value)
{
    ComboBoxItems items1=get_value().value<ComboBoxItems>();
    ComboBoxItems items2=value.value<ComboBoxItems>();

    return items1==items2;
}

qRegisterProperty(QComboBoxItemProperty)
